var t = require("../../../@babel/runtime/helpers/defineProperty"), a = getApp();

Component({
    properties: {},
    data: {
        show: !1,
        buttondisab: !0,
        cart_ids: [],
        showChoose: !1,
        dispatch_types: [],
        radioChoose: ""
    },
    lifetimes: {
        attached: function() {
            var t = wx.getStorageSync("langIndex");
            this.setData({
                language: t.en
            }), this.getCart();
        }
    },
    observers: {
        show: function(t) {
            t && (this.setData({
                cart_ids: []
            }), this.getCart());
        }
    },
    methods: {
        closeChoose: function() {
            this.setData({
                showChoose: !1
            });
        },
        toBuy: function() {
            this.submitGoods();
        },
        checkGoods: function(t) {
            var s = this;
            if (0 != this.data.cart_ids.length) if (console.log(this.data.cart_ids, "dddddd"), 
            this.data.cart_ids.length > 50) wx.showToast({
                icon: "none",
                title: "最多一次性只能下单50样商品",
                duration: 1500
            }); else {
                var e = a.getNetAddresss("memberCart.checkout");
                e += "&cart_ids=" + this.data.cart_ids.join(","), a._getNetWork({
                    url: e,
                    success: function(t) {
                        var a = t.data;
                        1 == a.result ? 1 == a.data.need_choose ? s.setData({
                            dispatch_types: a.data.dispatch_types,
                            showChoose: !0
                        }) : s.submitGoods() : wx.showModal({
                            title: "提示",
                            content: a.msg,
                            showCancel: !1,
                            success: function(t) {}
                        });
                    },
                    fail: function(t) {
                        console.log(t);
                    }
                });
            } else wx.showToast({
                icon: "none",
                title: "请选择结算商品",
                duration: 1500
            });
        },
        submitGoods: function() {
            var t = [];
            if (!this.data.showChoose || this.data.radioChoose.toString()) {
                if (this.data.showChoose) for (var a = 0; a < this.data.dispatch_types[this.data.radioChoose].member_carts.length; a++) t.push(this.data.dispatch_types[this.data.radioChoose].member_carts[a].id); else t = this.data.cart_ids;
                wx.navigateTo({
                    url: "/packageD/buy/myOrder_v2/myOrder_v2?tag=-1&cart_ids=" + JSON.stringify(t)
                });
            } else wx.showToast({
                icon: "none",
                title: "请先选择配送方式",
                duration: 1500
            });
        },
        typeChange: function(t) {
            this.setData({
                radioChoose: Number(t.detail)
            });
        },
        showTap: function() {
            this.setData({
                show: !this.data.show
            });
        },
        trggleCart: function() {
            this.setData({
                show: !1
            }), this.triggerEvent("cartShowClose", !0);
        },
        numberLeft1: function(t) {
            if (this.data.buttondisab) {
                this.setData({
                    buttondisab: !1
                }), wx.showLoading({
                    title: "删除商品中"
                });
                var a = t.currentTarget.dataset.item, s = t.currentTarget.dataset.index, e = a.cart_id, o = Number(this.data.carts[s].total) - 1;
                o <= 0 ? this.destroyData(e) : this.addCart(e, o);
            }
        },
        destroyData: function(t) {
            var s = this, e = a.getNetAddresss("member.member-cart.destroy"), o = {
                ids: t
            };
            a._getNetWork({
                url: e,
                data: o,
                success: function(e) {
                    var o = e.data;
                    if (wx.hideLoading({
                        success: function(t) {}
                    }), s.setData({
                        buttondisab: !0
                    }), 1 == o.result) {
                        wx.showToast({
                            title: o.msg,
                            icon: "none"
                        }), a.globalData.cart_num = a.globalData.cart_num - 1;
                        var i = s.data.cart_ids.indexOf(t);
                        i >= 0 && s.data.cart_ids.splice(i, 1), s.setData({
                            cart_ids: s.data.cart_ids
                        }), console.log(i, s.data.cart_ids, "dddddddddd"), s.getCart();
                    } else wx.showToast({
                        title: o.msg,
                        icon: "none"
                    });
                }
            });
        },
        numberRight1: function(t) {
            if (console.log(t), this.data.buttondisab) {
                this.setData({
                    buttondisab: !1
                }), wx.showLoading({
                    title: "添加商品中"
                });
                var a = t.currentTarget.dataset.item, s = t.currentTarget.dataset.index, e = a.cart_id, o = Number(this.data.carts[s].total) + 1;
                this.addCart(e, o);
            }
        },
        addCart: function(t, s) {
            var e = this, o = this, i = a.getNetAddresss("cart.list.updateNumV2");
            i += "&id=" + t, i += "&num=" + s, a._getNetWork({
                url: i,
                success: function(t) {
                    wx.hideLoading({
                        success: function(t) {}
                    });
                    var s = t.data;
                    1 == s.result ? (a.tips(s.msg), e.setData({
                        buttondisab: !0
                    }), o.getCart()) : (o.getCart(), wx.showToast({
                        title: s.msg,
                        icon: "none",
                        duration: 1500
                    }), e.setData({
                        buttondisab: !0
                    }));
                },
                fail: function(t) {
                    console.log(t), wx.hideLoading({
                        success: function(t) {}
                    });
                }
            });
        },
        clearCart: function() {
            var t = this;
            if (0 != this.data.cart_ids.length) {
                var s = a.getNetAddresss("cart.list.destroy");
                s += "&ids=" + this.data.cart_ids.join(","), a._getNetWork({
                    url: s,
                    success: function(a) {
                        var s = a.data;
                        1 == s.result ? t.getCart() : wx.showToast({
                            icon: "none",
                            title: s.msg,
                            duration: 1500
                        });
                    },
                    fail: function(t) {
                        console.log(t);
                    }
                });
            } else wx.showToast({
                icon: "none",
                title: "请选择删除商品",
                duration: 1500
            });
        },
        clickCount: function(t) {
            var a = t.currentTarget.dataset.total;
            this.setData({
                goodsCartsOld: a
            });
        },
        changeCount_2: function(a) {
            var s = a.detail.value, e = a.currentTarget.dataset.index;
            this.setData(t({}, "carts[" + e + "].total", s));
            var o = this.data.carts[e];
            if (o.total < 0) return wx.showToast({
                icon: "none",
                title: "商品数量不能为负数",
                duration: 1500
            }), void this.setData(t({}, "carts[" + e + "].total", this.data.goodsCartsOld));
            if (o.total < this.data.goodsCartsOld) {
                this.data.goodsCartsOld, o.total;
                this.addCart(o.cart_id, s);
            } else if (o.total > this.data.goodsCartsOld) {
                o.total, this.data.goodsCartsOld;
                this.addCart(o.cart_id, s);
            }
        },
        getCart: function() {
            var t = this, s = a.getNetAddresss("cart.list.otherPageCartList");
            a._getNetWork({
                url: s,
                success: function(a) {
                    var s = a.data;
                    if (1 == s.result) {
                        console.log(s.data, "2222222222");
                        var e = [], o = t.data.cart_ids;
                        if (s.data.list.length > 0) {
                            for (var i = 0; i < s.data.list.length; i++) for (var r = 0; r < s.data.list[i].carts.length; r++) e.push(s.data.list[i].carts[r]);
                            for (var d = 0; d < s.data.list.length; d++) for (var n = 0; n < s.data.list[d].carts.length; n++) o.indexOf(s.data.list[d].carts[n].cart_id) < 0 && o.push(s.data.list[d].carts[n].cart_id);
                        }
                        t.setData({
                            cart_ids: o,
                            carts: e,
                            total_amount: s.data.total_amount
                        }), console.log(t.data.cart_ids, "cart_ids");
                        for (var c = 0, h = 0; h < t.data.carts.length; h++) c += t.data.carts[h].total;
                        t.setData({
                            cartsNum: c
                        });
                    } else wx.showToast({
                        title: s.msg,
                        icon: "none"
                    });
                }
            });
        }
    }
});